# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 批量写入数据.py
# @Author: dongguangwen
# @Date  : 2025-05-25 15:37
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from datetime import datetime
import random
import time

es = Elasticsearch(hosts='http://192.168.1.119:9200')
# print(es)

names = ['刘一', '陈二', '张三', '李四', '王五', '赵六', '孙七', '周八', '吴九', '郑十']
sexs = ['男', '女']
age = [25, 28, 29, 32, 31, 26, 27, 30]
subjects = ['语文', '数学', '英语', '生物', '地理']
grades = [85, 77, 96, 74, 85, 69, 84, 59, 67, 69, 86, 96, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86]
character = ['自信但不自负,不以自我为中心',
             '努力、积极、乐观、拼搏是我的人生信条',
             '抗压能力强，能够快速适应周围环境',
             '敢做敢拼，脚踏实地；做事认真负责，责任心强',
             '爱好所学专业，乐于学习新知识；对工作有责任心；踏实，热情，对生活充满激情',
             '主动性强，自学能力强，具有团队合作意识，有一定组织能力',
             '忠实诚信,讲原则，说到做到，决不推卸责任',
             '有自制力，做事情始终坚持有始有终，从不半途而废',
             '肯学习,有问题不逃避,愿意虚心向他人学习',
             '愿意以谦虚态度赞扬接纳优越者,权威者',
             '会用100%的热情和精力投入到工作中；平易近人',
             '为人诚恳,性格开朗,积极进取,适应力强、勤奋好学、脚踏实地',
             '有较强的团队精神,工作积极进取,态度认真']
create_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
datas = []

start = time.time()
# 开始批量写入es数据库
# 批量写入数据
actions = []
for i in range(1000000):
    # print(i)
    data = {
            "_index": "grades_big",
            "_id": i,
            "_source": {
                "id": i,
                "name": random.choice(names) * 5,
                "sex": random.choice(sexs) * 3,
                "age": random.choice(age) * 35,
                "character": random.choice(character) * 11,
                "subject": random.choice(subjects) * 6,
                "grade": random.choice(grades) * 13,
                "time": create_time
            }
        }
    actions.append(data)
    if len(actions) >= 500:
        print(i)
        helpers.bulk(es, actions)
        actions = []
if len(actions) > 0:
    helpers.bulk(es, actions)
end = time.time()
print('花费时间：', end - start)
